//
//  FootPrintStoreVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/28.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class FootPrintStoreVC: BaseViewController {
    
    var urlStr : String = "-999"
    
    var hasmore = "-999"
    
    var page_total = "-999"
    
    var dataModels : [FootStoreModel]?
    
    
    
    var page = -999
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        plainTableView.tableFooterView = UIView()
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: FootPrintStoreCell.self), bundle: nil), forCellReuseIdentifier: FootPrintStoreCell.identifer())
        
        weak var weakSelf = self
        
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadFootPrintStoreData()
            
        }
        
        plainTableView.mj_header.beginRefreshing()
        
        plainTableView.mj_footer = ZMRefreshFooter.init{
            
            weakSelf!.loadMoreFootPrintStoreData()
            
        }
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   
}



// MARK:- 数据请求
extension FootPrintStoreVC{
    
    func loadFootPrintStoreData() {
        
        page = 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        
        
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: urlStr, parameters: parmeter) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                self?.hasmore = responseObject!["hasmore"] as! String
                
                self?.page_total = responseObject!["page_total"] as! String
                
                self?.dataModels = [FootStoreModel]()
                
                
                
                
                if self?.urlStr == "mobile/index.php?c=member_favorites&a=favorites_list" {
                    
                    
                    for dic in responseObject!["datas"]!["favorites_list"] as! [[String : AnyObject]]{
                        
                        self!.dataModels!.append(FootStoreModel(dic: dic))
                        
                    }
                    
                    
                }else{
                    
                    for dic in responseObject!["datas"]!["goodsbrowse_list"] as! [[String : AnyObject]]{
                        
                        self!.dataModels!.append(FootStoreModel(dic: dic))
                        
                    }
                    
                }
                
                self!.plainTableView.mj_footer.resetNoMoreData()
                
                
                if self?.hasmore == "0" {
                    
                    self!.plainTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                    
                }
                
                
                if self!.dataModels!.count == 0{
                    
                    if self?.urlStr == "mobile/index.php?c=member_favorites&a=favorites_list" {
                        
                        self?.showLogoView(text : "暂无收藏记录...")
                        
                    }else{
                        
                        self?.showLogoView(text : "暂无浏览记录...")
                        
                        
                    }
                    
                }
                
                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
    }
    
    
    
    
    
    
    func loadMoreFootPrintStoreData() {
        
        page += 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: urlStr, parameters: parmeter) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                self?.hasmore = responseObject!["hasmore"] as! String
                
                self?.page_total = responseObject!["page_total"] as! String
                
                if self?.urlStr == "mobile/index.php?c=member_favorites&a=favorites_list" {
                    
                    
                    for dic in responseObject!["datas"]!["favorites_list"] as! [[String : AnyObject]]{
                        
                        self!.dataModels!.append(FootStoreModel(dic: dic))
                        
                    }
                    
                    
                }else{
                    
                    for dic in responseObject!["datas"]!["goodsbrowse_list"] as! [[String : AnyObject]]{
                        
                        self!.dataModels!.append(FootStoreModel(dic: dic))
                        
                    }
                    
                }
                
                
                
                if self?.hasmore == "0" {
                    
                    self!.plainTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                    
                }else{
                    
                    self!.plainTableView.mj_footer.endRefreshing()
                    
                }
                
                
                
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_footer.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
    }
    
    
}






// MARK:- tableview代理方法
extension FootPrintStoreVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        if dataModels!.count > 0 {
            return 1
            
        } else {
            
            return 0
            
        }
        
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataModels?.count ?? 0
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let tableCell : FootPrintStoreCell =  tableView.dequeueReusableCell(withIdentifier: FootPrintStoreCell.identifer()) as! FootPrintStoreCell
        
        tableCell.model = dataModels?[indexPath.row]
        
        return tableCell
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 96
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        
        let temp = GoodsDetailController()
        
        temp.urlStr = dataModels![indexPath.row].detail_url
        
        navigationController?.pushViewController(temp, animated: true)
        
    }
    
    
    
}
